package com.dongjiantong.user.serviceImpl;/**
 * Created by cao on 2017/11/30.
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.mapper.RoleAppUserMapper;
import com.dongjiantong.user.model.RoleAppUser;
import com.dongjiantong.user.service.RoleAppUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cao
 * @create 2017-11-30 下午12:03
 **/
@Slf4j
//@Service(version = "1.0.0")
@Service(version = "1.0.0",interfaceName="com.dongjiantong.user.service.RoleAppUserService")
public class RoleAppUserServiceImpl  extends ServiceMybatis<RoleAppUser> implements RoleAppUserService {


    @Resource
   private RoleAppUserMapper roleAppUserMapper;

    @Override
   public List<RoleAppUser> queryRoleAppByUserId(Long userId)
   {
              //通用Example查询
       Example example = new Example(RoleAppUser.class);

       //这里给出的条件查询为id=100
       example.createCriteria()
               .andEqualTo("userId", userId)
               .andEqualTo("status",1);
       return roleAppUserMapper.selectByExample(example);
   }
   @Override
   @Transactional(propagation = Propagation.REQUIRED,rollbackFor={Exception.class, RuntimeException.class})
   public int saveRoleAppUser(Long userId,List<RoleAppUser> list)
   {
       int result=0;

       if(userId==0L)
       {
           return result;
       }

       //删除原数据
       roleAppUserMapper.deleteRoleAppUserByUserId(userId);

       //添加新数据
       result =  roleAppUserMapper.insertList(list);

       return  result;





   }

    @Override
    public List<Long> selectByRoleAndUserList(Integer role, List<Long> userIdList) {

        Example example=new Example(RoleAppUser.class);
        example.createCriteria().andIn("userId",userIdList).andEqualTo("appRole",role);
       List<RoleAppUser> roleAppUserList= roleAppUserMapper.selectByExample(example);
       List<Long> userList=new ArrayList<>();
       for (RoleAppUser roleAppUser:roleAppUserList){
           userList.add(roleAppUser.getUserId());
       }
        return userList;
    }
}
